<?php
class CommentsController extends AppController {

	var $name = 'Comments';
	var $helpers = array('Html', 'Form');
	
	function beforeFilter(){ 
        $this->__validateLoginStatus();
    }

    function __validateLoginStatus(){ 
       if($this->Session->check('User') == false){
          $this->Session->setFlash('you must be logged in to access this page');
          $this->redirect(array('controller' => 'users', 'action' => 'login'));
       }
    }

	function index() {
		$this->Comment->recursive = 0;
		$this->set('comments', $this->paginate());
	}

	
//	function view($id = null) {
//		if (!$id) {
//			$this->Session->setFlash(__('Invalid Comment.', true));
//			$this->redirect(array('action'=>'index'));
//		}
//		$comments = $this->Comment->read(null,$id);
//		$this->set('comment', $comments);
//	}
	
	
	function load_comments($parent_id) {
		$related_comments = $this->Comment->find('all',array(
														'conditions' => array('parent_id'=>$parent_id),
														'order' => array('Comment.created ASC'),
														'fields' => array(
															'Comment.id',
															'Comment.parent_id',
															'Comment.parent_type',
															'Comment.user_id',
															'Comment.content',
															'Comment.created',
															'Creator.id',
															'Creator.username'
														)
													)
												);
		return $related_comments;
	}

	function add($parent_id = -1, $parent_type = -1) {
		if (!empty($this->data)) {
			if ($this->data['Comment']['parent_id'] == -1 || $this->data['Comment']['parent_type'] == -1) {
				$this->redirect(array('controller' => HOME_CONTROLLER, 'action' => 'index'));
			}
			$this->Comment->create();
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('The Comment has been added successfully', true));
				
				if ($this->data['Comment']['parent_type'] == 1) {
					$this->redirect(array('controller' => 'events', 'action'=>'view', $this->data['Comment']['parent_id']));
				}
			} else {
				$this->Session->setFlash(__('The Comment could not be saved. Please, try again.', true));
				$this->set('parent_id', $this->data['Comment']['parent_id']);
				$this->set('parent_type', $this->data['Comment']['parent_type']);
			}
		}
		else {
			if ($parent_id == -1 || $parent_type == -1) {
				$this->redirect(array('controller' => HOME_CONTROLLER, 'action' => 'index'));
			}
			$this->set('parent_id', $parent_id);
			$this->set('parent_type', $parent_type);
		}
		$this->set('user_id', $this->Session->read('User.id'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Comment', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->__validateUser($id);
		if (!empty($this->data)) {
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('The Comment has been edited successfully', true));
				if ($this->data['Comment']['parent_type'] == 1) {
					$this->redirect(array('controller' => 'events', 'action'=>'view', $this->data['Comment']['parent_id']));
				}
			} else {
				$this->Session->setFlash(__('The Comment could not be saved. Please, try again.', true));
				$this->data = $this->Comment->read(null, $id);
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Comment->read(null, $id);
		}
	}
	
	function __validateUser($comment_id) {
		$user_id = $this->Comment->read(null, $comment_id);
		$user_id = $user_id['Comment']['user_id'];
		$current_user_id = $this->Session->read('User.id');
		if ($user_id != $current_user_id) {
			$this->Session->setFlash(__('You can not access this comment, you are not the owner of it.', true));
			$this->redirect(array('action' =>'index'));
		}
	}

	function delete($id = null, $parent_id = null, $parent_type = null) {
		if (!$id || !$parent_id) {
			$this->Session->setFlash(__('Invalid id for Comment', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Comment->del($id)) {
			$this->Session->setFlash(__('Comment deleted successfully', true));
			if ($parent_type == 1) {
				$this->redirect(array('controller' => 'events', 'action'=>'view', $parent_id));
			}
			return;
		}
	}
	
	function report($id = null, $parent_id = null, $parent_type = null) {
		if (!$id || !$parent_id || !$parent_type) {
			$this->Session->setFlash(__('Invalid id for Comment', true));
			$this->redirect(array('action'=>'index'));
		}
		$comment = $this->Comment->find('first', array('conditions' => array('Comment.id' => $id)));
		$comment['Comment']['is_reported'] ++;
		$this->Comment->save($comment);
		if ($parent_type == 1) {
			$this->redirect(array('controller' => 'events', 'action' => 'view', $parent_id));
		}
		return;
	}
	
	function load_reported() {
		
		$reportedComments = $this->Comment->find('all',array('conditions'=>array('Comment.is_reported > ' => 0),'order'=>array('Comment.is_reported DESC')));
		$this->set('parentArray', array(1 => 'events', 2 => 'ideas'));
		debug($reportedComments);
		 
	}
	
	function parent_type_name($parent_type) {
		switch($parent_type) {
			case 1:
				return "events";
			case 2:
				return "ideas";
		}
	}

}
?>